package com.xm.controller;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.io.IOException;
import java.util.Objects;

@RestController
@RequestMapping("/api/log")
public class LogController {
    ResponseBodyEmitter gEmitter = new ResponseBodyEmitter(0L);
    @GetMapping("/events")
    public ResponseEntity<ResponseBodyEmitter> handle() {
        ResponseBodyEmitter emitter = new ResponseBodyEmitter(0L);
        gEmitter = emitter;
        return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(emitter);
    }
    @GetMapping("/sendEmitter")
    public void sendEmitter(@RequestParam("str") String str) throws IOException {
        if (Objects.equals("end", str)) {
            gEmitter.complete();
        } else {
            gEmitter.send(str);
        }
    }
}
